﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using DeepEarth.Client.MapControl.Layers;

namespace DeepEarth.Client.Services.ArcGIS
{
    public enum ArcGISMapModes
    {
        World_Imagery,
        World_Street_Map,
        World_Topo_Map,
        World_Terrain_Base,
        World_Shaded_Relief,
        World_Physical_Map,
        USA_Topo_Maps,
        DeLorme_World_Base_Map,
        World_Navigation_Charts,
        World_Reference_Overlay,
        World_Boundaries_and_Places_Alternate,
        World_Transportation,
        USA_Unemployment_Rate,
        USA_Tapestry,
        USA_Social_Vulnerability_Index,
        USA_Retail_Spending_Potential,
        USA_Recent_Population_Change,
        USA_Projected_Population_Change,
        USA_Population_Density,
        USA_Percent_Under_18,
        USA_Percent_Over_64,
        USA_Percent_Male,
        USA_Owner_Occupied_Housing,
        USA_Median_Net_Worth,
        USA_Median_Household_Income,
        USA_Median_Home_Value,
        USA_Median_Age,
        USA_Labor_Force_Participation_Rate,
        USA_Diversity_Index,
        USA_Daytime_Population,
        USA_Average_Household_Size,
        USA_1990_2000_Population_Change
    }

    public class ArcGisTileSource : TileSource
    {
        // Fields
        private bool _isInitialized;
        private bool _isTileDownloadStarted;
        private ArcGISMapModes _mapMode; 
        private const string TilePath = "http://services.arcgisonline.com/ArcGIS/rest/services/{maptype}{map}/MapServer/tile/{z}/{y}/{x}.jpg";

        
        // Methods
        public ArcGisTileSource()
        {
        }

        public ArcGisTileSource(ArcGISMapModes mode)
        {
            this.MapMode = mode;
        }

        public override UIElement GetCopyright()
        {
            if (this.MapMode == ArcGISMapModes.World_Street_Map)
            {
                return new Image { Source = new BitmapImage(new Uri("http://media.amazonwebservices.com/Powered-by-Amazon-Web-Services.jpg")), Stretch = Stretch.None };
            }
            return null;
        }

        public override Uri GetTile(int tileLevel, int tilePositionX, int tilePositionY)
        {
            if (!this.IsInitialized)
            {
                return null;
            }
            this._isTileDownloadStarted = true;
            int num = this.TileToZoom(tileLevel);
            string uriString = "http://services.arcgisonline.com/ArcGIS/rest/services/{maptype}{map}/MapServer/tile/{z}/{y}/{x}.jpg";
            string newValue = string.Empty;
            switch (this.MapMode)
            {
                case ArcGISMapModes.DeLorme_World_Base_Map:
                    newValue = "Specialty/";
                    break;

                case ArcGISMapModes.World_Navigation_Charts:
                    newValue = "Specialty/";
                    break;

                case ArcGISMapModes.World_Reference_Overlay:
                    newValue = "Reference/";
                    break;

                case ArcGISMapModes.World_Boundaries_and_Places_Alternate:
                    newValue = "Reference/";
                    break;

                case ArcGISMapModes.World_Transportation:
                    newValue = "Reference/";
                    break;

                case ArcGISMapModes.USA_Unemployment_Rate:
                    newValue = "Demographics/";
                    break;

                case ArcGISMapModes.USA_Tapestry:
                    newValue = "Demographics/";
                    break;

                case ArcGISMapModes.USA_Social_Vulnerability_Index:
                    newValue = "Demographics/";
                    break;

                case ArcGISMapModes.USA_Retail_Spending_Potential:
                    newValue = "Demographics/";
                    break;

                case ArcGISMapModes.USA_Recent_Population_Change:
                    newValue = "Demographics/";
                    break;

                case ArcGISMapModes.USA_Projected_Population_Change:
                    newValue = "Demographics/";
                    break;

                case ArcGISMapModes.USA_Population_Density:
                    newValue = "Demographics/";
                    break;

                case ArcGISMapModes.USA_Percent_Under_18:
                    newValue = "Demographics/";
                    break;

                case ArcGISMapModes.USA_Percent_Over_64:
                    newValue = "Demographics/";
                    break;

                case ArcGISMapModes.USA_Percent_Male:
                    newValue = "Demographics/";
                    break;

                case ArcGISMapModes.USA_Owner_Occupied_Housing:
                    newValue = "Demographics/";
                    break;

                case ArcGISMapModes.USA_Median_Net_Worth:
                    newValue = "Demographics/";
                    break;

                case ArcGISMapModes.USA_Median_Household_Income:
                    newValue = "Demographics/";
                    break;

                case ArcGISMapModes.USA_Median_Home_Value:
                    newValue = "Demographics/";
                    break;

                case ArcGISMapModes.USA_Median_Age:
                    newValue = "Demographics/";
                    break;

                case ArcGISMapModes.USA_Labor_Force_Participation_Rate:
                    newValue = "Demographics/";
                    break;

                case ArcGISMapModes.USA_Diversity_Index:
                    newValue = "Demographics/";
                    break;

                case ArcGISMapModes.USA_Daytime_Population:
                    newValue = "Demographics/";
                    break;

                case ArcGISMapModes.USA_Average_Household_Size:
                    newValue = "Demographics/";
                    break;

                case ArcGISMapModes.USA_1990_2000_Population_Change:
                    newValue = "Demographics/";
                    break;
            }
            uriString = uriString.Replace("{maptype}", newValue).Replace("{map}", this.MapMode.ToString()).Replace("{x}", tilePositionX.ToString()).Replace("{y}", tilePositionY.ToString()).Replace("{z}", num.ToString());
            if (this.MapMode == ArcGISMapModes.USA_1990_2000_Population_Change)
            {
                uriString = uriString.Replace("USA_1990_2000_Population_Change", "USA_1990-2000_Population_Change");
            }
            return new Uri(uriString);
        }

        // Properties
        public override bool IsInitialized
        {
            get
            {
                return this._isInitialized;
            }
        }

        public ArcGISMapModes MapMode
        {
            get
            {
                return this._mapMode;
            }
            set
            {
                if (this._isTileDownloadStarted)
                {
                    throw new InvalidOperationException();
                }
                this._mapMode = value;
                base.Id = value.ToString();
                this._isInitialized = true; 
            }
        }

        public override Color TileColor
        {
            get
            {
                return Colors.Black;
            }
        }
    }


}
